/**
 * session用户监控，用于增减用户活跃度（活跃度超过限值会封禁）
 */

var router = require('express').Router();
var assertAlljs = require('../bin/util/assert-alljs');
var userTool = require('../bin/user');
var redisentityTool = require('../bin/redisentity');

router.post('/makeactive', async function(req, res) {
    let assert = assertAlljs(req, res);
    let checkMessage = userTool.check(req.session.user, {role:'admin'});
    if (await assert(checkMessage))
        return;
    
    let plus = parseFloat(req.body.plus);
    if (isNaN(plus)){
        res.end();
        return;
    }

    if (req.body.ip){
        let ipspec = await redisentityTool.get('active', req.body.ip);
        if (ipspec instanceof Error || ipspec == null){
            res.end();
            return;
        }
        ipspec.active += plus;
        await redisentityTool.set('active', req.body.ip, ipspec);

    } else if (req.body.ses_id){
        let ses = await redisentityTool.get('ses', req.body.ses_id);
        if (ses instanceof Error || ses == null){
            res.end();
            return;
        }
        ses.active += plus;
        await redisentityTool.set('ses', req.body.ses_id, ses);

    }
    res.end();
});

module.exports = router;